﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessLayer.RegInfo.Models;
using DBHelper;
using BusinessLayer.RegInfo.Entities;
using System.Data.SqlClient;
using System.Data;

namespace BusinessLayer.RegInfo
{
    public class AccountBL
    {
        public void Insert(Account account, int subscriptionId, int typePay, int typeTransaction, string couponCode, string merchTxnRef, out double amount, out long transactionId, out int result)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);            
            adapter.AddParameter("@FirstName", account.FirstName);
            adapter.AddParameter("@LastName", account.LastName);
            adapter.AddParameter("@Email", account.Email);
            adapter.AddParameter("@PassWord", account.PassWord);
            adapter.AddParameter("@CountryId", account.CountryId);
            adapter.AddParameter("@Status", account.Status);
            adapter.AddParameter("@SubscriptionId", subscriptionId);
            adapter.AddParameter("@TypePay", typePay);
            adapter.AddParameter("@TypeTransaction", typeTransaction);
            adapter.AddParameter("@CouponCode", couponCode);
            adapter.AddParameter("@MerchTxnRef", merchTxnRef);
            var outputAmount = adapter.AddParameter("@Amount", SqlDbType.Float, ParameterDirection.Output);
            var outputTransactionId = adapter.AddParameter("@TransactionId", SqlDbType.BigInt, ParameterDirection.Output);
            var outputResult = adapter.AddParameter("@Result", SqlDbType.Int, ParameterDirection.Output);
            adapter.ExecuteNonQuery("sp_Account_Insert", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            amount = (double)outputAmount.Value;
            transactionId = (long)outputTransactionId.Value;
            result = (int)outputResult.Value;
        }

        public void Update(Account account)
        {
            Update(account, null);
        }

        public void Update(Account account, int? expiresDate)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@Id", account.Id);
            adapter.AddParameter("@FirstName", account.FirstName);
            adapter.AddParameter("@LastName", account.LastName);
            adapter.AddParameter("@Email", account.Email);
            adapter.AddParameter("@PassWord", account.PassWord);
            adapter.AddParameter("@CountryId", account.CountryId);
            adapter.AddParameter("@CreateDate", account.CreateDate);
            adapter.AddParameter("@ExpiresDate", expiresDate);
            adapter.AddParameter("@Status", account.Status);
            adapter.ExecuteNonQuery("sp_Account_Update", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
        }

        public void Delete(int accountId)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@Id", accountId);            
            adapter.ExecuteNonQuery("sp_Account_Delete", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);            
        }

        public Account GetByEmail(string email)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);            
            adapter.AddParameter("@Email", email);

            var list = adapter.GetList<Account>("sp_Account_Select_By_Email", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list.FirstOrDefault();
        }

        public Account GetByMerchTxnRef(string merchTxnRef)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);            
            adapter.AddParameter("@MerchTxnRef", merchTxnRef);

            var list = adapter.GetList<Account>("sp_Account_Select_By_MerchTxnRef", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list.FirstOrDefault();
        }

        public Account GetById(int accountId)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);            
            adapter.AddParameter("@Id", accountId);

            var list = adapter.GetList<Account>("sp_Account_Select_By_Id", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            return list.FirstOrDefault();
        }

        public void ActiveAccount(long accountId ,out int result)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@AccountId", accountId);
            //adapter.AddParameter("@TokenKey", tokenKey);
            //adapter.AddParameter("@MerchTxnRef", merchTxnRef);
            var output = adapter.AddParameter("@Result", SqlDbType.Int, ParameterDirection.Output);            
            adapter.ExecuteNonQuery("sp_ActiveAccount", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            result = (int)output.Value;
        }

        public List<AccountExtend> Search(string firstName, string lastName, string email, int? countryId, int? status, int? partnerId, DateTime? fromDate, DateTime? toDate, int? typePay, int pageIndex, int pageSize, out int totalRecord)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@FirstName", firstName);
            adapter.AddParameter("@LastName", lastName);
            adapter.AddParameter("@Email", email);
            adapter.AddParameter("@CountryId", countryId);
            adapter.AddParameter("@Status", status);
            adapter.AddParameter("@PartnerId", partnerId);
            adapter.AddParameter("@FromDate", fromDate);
            adapter.AddParameter("@ToDate", toDate);
            adapter.AddParameter("@TypePay", typePay);
            adapter.AddParameter("@PageIndex", pageIndex);
            adapter.AddParameter("@PageSize", pageSize);
            var output = adapter.AddParameter("@TotalRecord", SqlDbType.Int, ParameterDirection.Output);
            var list = adapter.GetList<AccountExtend>("sp_Account_Search", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            totalRecord = Convert.ToInt32(output.Value);
            return list;
        }

        public void InsertBlackList(string email)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@Email", email);
            adapter.ExecuteNonQuery("sp_AccountBlackList_Insert", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
        }

        public void DeleteBlackList(int accountId)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            adapter.AddParameter("@AccountId", accountId);
            adapter.ExecuteNonQuery("sp_AccountBlackList_Delete", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
        }

        public List<AccountExtend> SearchBlackList(string email, int pageIndex, int pageSize, out int totalRecord)
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);          
            adapter.AddParameter("@Email", email);           
            adapter.AddParameter("@PageIndex", pageIndex);
            adapter.AddParameter("@PageSize", pageSize);
            var output = adapter.AddParameter("@TotalRecord", SqlDbType.Int, ParameterDirection.Output);
            var list = adapter.GetList<AccountExtend>("sp_AccountBlackList_Search", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            totalRecord = Convert.ToInt32(output.Value);
            return list;
        }

        public List<AccountExtend> GetAllAccountNotInBlackList()
        {
            var adapter = new BaseDAO(DbConfig.DbRegInfoConnectionString);
            var list = adapter.GetList<AccountExtend>("sp_Account_Select_All_Not_In_Black_List", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);            
            return list;
        }
    }
}
